import { _decorator, find, Label, Node, ScrollView, size, Sprite, SpriteFrame } from 'cc';
import { BaseLayer } from './BaseLayer';
import { SdkMgr } from '../manager/SdkMgr';
import { AudioMgr } from '../manager/AudioMgr';
import { ENUM_AUDIO_CLIP, ENUM_UI_LAYER } from '../Enum';
import { Ins } from '../Ins';
import { EffMgr } from '../manager/EffMgr';
import { PoolMgr } from '../manager/PoolMgr';
import { DataMgr } from '../manager/DataMgr';
import { ReqMgr } from '../manager/ReqMgr';
import { UIScroll } from '../UIScroll';
import { wxAvatar } from '../Util';
import { ResMgr } from '../manager/ResMgr';
const { ccclass, property } = _decorator;

@ccclass('RankLayer')
export class RankLayer extends BaseLayer {
    btn_close: Node = null
    btn_share: Node = null
    scroll: Node = null
    tip: Node = null
    item: Node = null
    avatars = new Map<string, SpriteFrame>()

    onLoad() {
        this.btn_close = find('panel/header/btn_close', this.node)
        this.btn_close.on('click', this.onCloseClick, this)
        this.btn_share = find('panel/header/btn_share', this.node)
        this.btn_share.on('click', this.onShareClick, this)
        this.scroll = find('panel/main/scroll', this.node)
        this.tip = find('panel/tip', this.node)
        this.item = PoolMgr.ins.getNode('RankItem')
    }

    onDestroy() {
        this.btn_close.off('click', this.onCloseClick, this)
        this.btn_share.off('click', this.onShareClick, this)
    }

    onEnable() {
        SdkMgr.ins.toggleCustomRowAd(true)
        this.rendor()
    }

    onDisable() {
        super.onDisable()
        SdkMgr.ins.toggleCustomRowAd(false)
    }

    async rendor() {
        if (!SdkMgr.ins.getPlatform()) return
        if (!DataMgr.ins.user) {
            Ins.UIMgr.toggle(ENUM_UI_LAYER.RANK, false)
            Ins.UIMgr.toggle(ENUM_UI_LAYER.AUTH)
            return
        }
        SdkMgr.ins.showInterstitialAd()
        const res = await ReqMgr.ins.request('rank-get', 'get', { openid: DataMgr.ins.user.openid })
        // const res = {
        //     top: [
        //         {
        //             nickname: 123,
        //             rank: 1,
        //             avatar: '',
        //             score: 10
        //         },
        //         {
        //             nickname: 222,
        //             rank: 1,
        //             avatar: '',
        //             score: 10
        //         },
        //         {
        //             nickname: 333,
        //             rank: 1,
        //             avatar: '',
        //             score: 10
        //         },
        //         {
        //             nickname: 444,
        //             rank: 1,
        //             avatar: '',
        //             score: 10
        //         },
        //         {
        //             nickname: 555,
        //             rank: 1,
        //             avatar: '',
        //             score: 10
        //         },
        //         {
        //             nickname: 666,
        //             rank: 1,
        //             avatar: '',
        //             score: 10
        //         }
        //     ],
        //     my: []
        // }
        // console.log(res)
        const top = res['top']
        const my = res['my']
        let isScrollTop = false
        this.scroll.getComponent(UIScroll).init(this.item, top.length, size(520, 105), 0, (node: Node, index: number) => {
            if (!isScrollTop) {
                isScrollTop = true
                this.scroll.getComponent(ScrollView).scrollToTop()
            }
            const data = top[index]
            // 名次
            const rank = node.getChildByName('rank')
            rank.getChildByName('icon1').active = false
            rank.getChildByName('icon2').active = false
            rank.getChildByName('icon3').active = false
            rank.getChildByName('label').getComponent(Label).string = `${data.rank}`
            if (data.rank <= 3) rank.getChildByName(`icon${data.rank}`).active = true
            // 昵称
            const nickname = node.getChildByName('nickname')
            nickname.getComponent(Label).string = `${data.nickname}`
            // 头像
            const avatar = node.getChildByName('avatar')
            if (data.avatar) {
                if (!this.avatars.has(data.avatar)) {
                    wxAvatar(data.avatar).then((texture: any) => {
                        const spriteFrame = new SpriteFrame()
                        spriteFrame.texture = texture
                        avatar.getComponent(Sprite).spriteFrame = spriteFrame
                        this.avatars.set(data.avatar, spriteFrame)
                    })
                } else {
                    const spriteFrame = this.avatars.get(data.avatar)
                    avatar.getComponent(Sprite).spriteFrame = spriteFrame
                }
            } else {
                avatar.getComponent(Sprite).spriteFrame = ResMgr.ins.getSprite('avatar_default')
            }
            // 闯关进度
            const msg = node.getChildByName('msg')
            msg.getComponent(Label).string = `脑力值：${data.score}`
        }, (scroll: ScrollView) => {
            scroll.scrollToTop()
        })
        // 个人排行
        if (my.length) {
            this.tip.getComponent(Label).string = `你的排名：第${my[0]['rank']}名`
        } else {
            this.tip.getComponent(Label).string = '您尚未上榜'
        }
    }

    async onCloseClick() {
        AudioMgr.ins.playSound(ENUM_AUDIO_CLIP.CLICK)
        await Ins.FadeMgr.fadeIn()
        Ins.UIMgr.toggle(ENUM_UI_LAYER.RANK, false)
        Ins.UIMgr.toggle(ENUM_UI_LAYER.MENU)
    }

    onShareClick() {
        AudioMgr.ins.playSound(ENUM_AUDIO_CLIP.CLICK)
        if (!SdkMgr.ins.getPlatform()) {
            EffMgr.ins.showTip('EffTip', '该功能仅支持小游戏平台')
            return
        }
        SdkMgr.ins.activeShare()
    }

}


